/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmSearchCus.java
 *
 * Created on May 22, 2011, 7:59:43 AM
 */

package Ordermanagement;


import Account.SizeMonition;
import Connections.DataTools;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author BachDEV
 */
public class frmSearchCus extends javax.swing.JFrame {

    Vector vec = new Vector();
    Vector vecAdd = new Vector();
    DataTools dt = new DataTools();
    JTable tabCus = null;
    String name = "";
    frmOrder order;
    public static int x ;
    public static int y ;

    /** Creates new form frmSearchCus */
   public frmSearchCus(frmOrder order) {

        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(InstantiationException ex){
            ex.printStackTrace();
        }catch(UnsupportedLookAndFeelException ex){
            ex.printStackTrace();
        }catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }catch(IllegalAccessException ex){
            ex.printStackTrace();
        }
        try
        {
            Thread.sleep(1000);
            initComponents();
            Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
            x = this.getWidth();
            y = this.getHeight();
            this.setLocation((ds.width - this.getWidth()) / 2, (ds.height - this.getHeight()) / 2);
            order.setEnabled(false);
            this.order = order;
            btnAdd.setEnabled(false);
            vec = dt.getCustormer();
            showCus(vec);
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }

    }
   public void motion() {
        this.setSize(0, 0);
        new Thread(new SizeMonition(this, x, y)).start();
    }
   ///////////////////////////////////////////////////////////////////////////
   public void showCus(Vector ve)
    {
        try
        {
            Vector vec = new Vector();
            vec.add("ID");
            vec.add("Name");
            vec.add("Address");
            vec.add("Phone");
            vec.add("Email");
            vec.add("Status");
            vec.add("Dispatch No");
            tabCus = new JTable(ve, vec);
            tabCus.getColumnModel().getColumn(0).setMinWidth(0);
            tabCus.getColumnModel().getColumn(0).setMaxWidth(0);
            scrSearchCus.setViewportView(tabCus);
            btnAdd.setEnabled(false);
            clickCus();
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void clickCus()
    {
         tabCus.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                btnAdd.setEnabled(true);
                int rowSelect = tabCus.getSelectedRow();
                for(int i = 0; i< 7; i++)
                {
                    vecAdd.add(tabCus.getValueAt(rowSelect, i));

                }

            }

            public void mousePressed(MouseEvent e) {

            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mouseEntered(MouseEvent e) {

            }

            public void mouseExited(MouseEvent e) {

            }
        });

        tabCus.addKeyListener(new KeyListener() {

            public void keyTyped(KeyEvent e) {

            }

            public void keyPressed(KeyEvent e) {

            }

            public void keyReleased(KeyEvent e) {
                btnAdd.setEnabled(true);
                int rowSelect = tabCus.getSelectedRow();
                for(int i = 0; i< 7; i++)
                {
                    vecAdd.add(tabCus.getValueAt(rowSelect, i));
                }
            }
        });
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        scrSearchCus = new javax.swing.JScrollPane();
        jLabel5 = new javax.swing.JLabel();
        cbSearchBy = new javax.swing.JComboBox();
        txtSearch = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Search Customer");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel4.setText("Search Custormer");

        jLabel5.setText("Search by:");

        cbSearchBy.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Phone", "Email", "Dispatch" }));
        cbSearchBy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbSearchByActionPerformed(evt);
            }
        });

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrSearchCus, javax.swing.GroupLayout.DEFAULT_SIZE, 646, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(231, 231, 231)
                .addComponent(jLabel4)
                .addContainerGap(275, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbSearchBy, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(96, 96, 96))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(187, Short.MAX_VALUE)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(102, 102, 102)
                .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(196, 196, 196))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(11, 11, 11)
                .addComponent(scrSearchCus, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSearch)
                    .addComponent(cbSearchBy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnExit))
                .addGap(47, 47, 47))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbSearchByActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbSearchByActionPerformed
        txtSearch.setText("");
}//GEN-LAST:event_cbSearchByActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        try {
            Vector ve = new Vector();
            String phone = cbSearchBy.getSelectedItem().toString();
            String email = cbSearchBy.getSelectedItem().toString();
            String dispatch = cbSearchBy.getSelectedItem().toString();
            if("Phone".equalsIgnoreCase(phone)) {
                if("".equalsIgnoreCase(txtSearch.getText())) {
                    JOptionPane.showMessageDialog(this, "Phone is not empty");
                    return;
                }
                try {
                    double dphone = Double.parseDouble(txtSearch.getText());
                } catch(Exception ex) {
                    JOptionPane.showMessageDialog(this, "Phone only can number");
                }
                String sphone = txtSearch.getText();
                ve = dt.searchCus(sphone, 1);
                showCus(ve);
            }

            if("Email".equalsIgnoreCase(email)) {
                if("".equalsIgnoreCase(txtSearch.getText())) {
                    JOptionPane.showMessageDialog(this, "Email is not null");
                    return;
                }
                String sEmail = txtSearch.getText();
                ve = dt.searchCus(sEmail, 2);
                showCus(ve);
            }

            if("Dispatch".equalsIgnoreCase(dispatch)) {
                if("".equalsIgnoreCase(txtSearch.getText())) {
                    JOptionPane.showMessageDialog(this, "Dispatch is not null");
                    return;
                }
                String sDispatch = txtSearch.getText();
                System.out.println("Dispatch: " + sDispatch);
                ve = dt.searchCus(sDispatch, 3);
                showCus(ve);
            }

        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
            return;
        }
}//GEN-LAST:event_btnSearchActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        this.setVisible(false);
        order.setEnabled(true);
        order.setAlwaysOnTop(true);
}//GEN-LAST:event_btnExitActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        //System.out.println("name: "+name);
        order.cusID = Integer.parseInt(vecAdd.get(0).toString());
        order.txtName.setText(vecAdd.get(1).toString());
        order.txtPhone.setText(vecAdd.get(3).toString());
        order.txaAddress.setText(vecAdd.get(2).toString());
        dispose();
}//GEN-LAST:event_btnAddActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        order.setEnabled(true);
        order.setAlwaysOnTop(true);
    }//GEN-LAST:event_formWindowClosed

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbSearchBy;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane scrSearchCus;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables

}
